package com.ruoyi.rva.extension.impl;

import com.ruoyi.rva.domain.RvaApp;
import com.ruoyi.rva.domain.RvaMap;
import com.ruoyi.rva.domain.RvaObject;
import com.ruoyi.rva.domain.RvaView;
import com.ruoyi.rva.extension.RvaDeleteInterceptor;
import com.ruoyi.rva.extension.RvaFormSubmitInterceptor;
import com.ruoyi.rva.mapper.RvaObjectMapper;
import com.ruoyi.rva.mapper.RvaSchemaMapper;
import com.ruoyi.rva.service.IRvaMetaService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component(RvaDeleteInterceptor.BEAN_FREFIX + "l1_rva_object")
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class RvaObjectDeleteInterceptor implements RvaDeleteInterceptor {

    private final IRvaMetaService metaService;

    private final RvaSchemaMapper schemaMapper;

    private final RvaObjectMapper objectMapper;

    @Override
    public Boolean preHandle(String keyValue, Map selection, RvaObject object, RvaView list, RvaMap req) {
        RvaObject o = objectMapper.selectRvaObjectById(keyValue);
        schemaMapper.execute(RvaObject.getDropTableSql(o));
        metaService.deleteObject(keyValue);
        return false;
    }

    @Override
    public void postHandle(String keyValue, Map selection, RvaObject object, RvaView list, RvaMap req) {

    }
}
